package redis

import (
	"context"
	"fmt"
	"time"
)

const (
	SHOP_INCREASE = "shop:increase"
)

type shop struct {
}

var Shop = &shop{}

func (e *shop) AddOneDay(ctx context.Context, key time.Time, count int64) (int64, error) {
	ketStr := key.Format("2006-01")
	return DashboardCache.AddOneDayCount(ctx, SHOP_INCREASE, ketStr, count)

}
func (e *shop) GetOneDay(ctx context.Context, key time.Time) (int64, error) {
	keyStr := key.Format("2006-01")
	dayOfMonth := key.Day()
	// 月份天数都是从1开始的，list从0开始
	return DashboardCache.GetOneDayCount(ctx, SHOP_INCREASE, keyStr, int64(dayOfMonth-1))
}

func (e *shop) AddThirtyDays(ctx context.Context, key time.Time, counts []int64) (int64, error) {
	keyStr := key.Format("2006-01")
	return DashboardCache.AddBatch(ctx, SHOP_INCREASE, keyStr, counts...)
}

// GetThirtyDays 获取当前月份所有的天数数据
func (e *shop) GetThirtyDays(ctx context.Context, key time.Time) ([]int64, error) {
	keyStr := key.Format("2006-01")
	return DashboardCache.GetListItems(ctx, SHOP_INCREASE, keyStr)
}

// AddTwelveMonths 获取当前年所有月份数据
func (e *shop) AddTwelveMonths(ctx context.Context, key time.Time, counts []int64) (int64, error) {
	keyStr := key.Format("2006")
	return DashboardCache.AddBatch(ctx, SHOP_INCREASE, keyStr, counts...)
}

// GetTwelveMonths 获取当前年所有月份数据
func (e *shop) GetTwelveMonths(ctx context.Context, key time.Time) ([]int64, error) {
	keyStr := key.Format("2006")
	return DashboardCache.GetListItems(ctx, SHOP_INCREASE, keyStr)
}

// AddFiveYears 获取当前年所有月份数据
func (e *shop) AddFiveYears(ctx context.Context, key time.Time, counts []int64) (int64, error) {
	// 前面4年的数据是已经确定的，年度数据实时计算redis内部缓存数据
	// 往前推4年
	keyStr := fmt.Sprintf("%s:%s", "years", key.Format("2006"))
	return DashboardCache.AddBatch(ctx, SHOP_INCREASE, keyStr, counts...)
}

func (e *shop) GetFiveYears(ctx context.Context, key time.Time) ([]int64, error) {
	// 按照从远到近依次返回数据，不包含今年的数据，今年数据实时计算
	keyStr := fmt.Sprintf("%s:%s", "years", key.Format("2006"))
	return DashboardCache.GetListItems(ctx, SHOP_INCREASE, keyStr)
}
